package cn.gzsxt.music.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzsxt.music.mapper.LableMapper;
import cn.gzsxt.music.mapper.SongListMapper;
import cn.gzsxt.music.mapper.SsListMapper;
import cn.gzsxt.music.mapper.UserMapper;
import cn.gzsxt.music.query.LableQueryObject;
import cn.gzsxt.music.query.SongListQueryObject;
import cn.gzsxt.music.query.UserQueryObject;
import cn.gzsxt.music.service.SongListService;

@Service
public class SongListServiceImpl implements SongListService {

	@Autowired
	private SongListMapper songListMapper;

	@Autowired
	private LableMapper labelMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private SsListMapper SsListMapper;

	@Override
	public Map<String, Object> selectSongListData(SongListQueryObject qo) {
		Map<String, Object> songListMap = new HashMap<>();
		songListMap.put("total", songListMapper.selectSongListTotal(qo));
		List<Map<String, Object>> temp = songListMapper.selectSongList(qo);

		Integer avgLike = songListMapper.getAvgLike();

		for (int i = 0; i < temp.size(); i++) {
			String label = (String) temp.get(i).get("songList_label");
			Integer listLike = (Integer) temp.get(i).get("songList_like");
			Integer songList_id = (Integer) temp.get(i).get("songList_id");
			Integer songList_hit = (Integer) temp.get(i).get("songList_hit");
			Integer realLike = songListMapper.selectTotleLikeById(songList_id.toString());
			Integer hit = 0;
			if (realLike != null) {
				if (realLike > avgLike) {
					hit += 60;
				} else {
					hit += 30;
				}
				if (songList_hit != null) {
					if (hit > songList_hit) {
						temp.get(i).put("songList_hit", (Object) hit);
					}
				} else {
					temp.get(i).put("songList_hit", (Object) hit);
				}
			}
			if (StringUtils.isNotBlank(label)) {
				temp.get(i).put("songList_label", songListMapper.selectLabelName(label).get(0).get("songList_label"));
			}
			if (listLike != null) {
				if (realLike != null) {
					if (realLike > listLike) {
						temp.get(i).put("songList_like", (Object) realLike);
					}
				}
			} else {
				if (realLike != null) {
					temp.get(i).put("songList_like", (Object) realLike);
				}
			}
		}
		songListMap.put("rows", temp);
		return songListMap;
	}

	@Override
	public Map<String, Object> getAllUser(UserQueryObject qo) {
		Map<String, Object> getAllUserMap = new HashMap<>();
		getAllUserMap.put("total", userMapper.selectUserTotalByQo(qo));
		getAllUserMap.put("rows", userMapper.selectUserListByQo(qo));
		return getAllUserMap;
	}

	@Override
	public Map<String, Object> getAllSong(LableQueryObject qo) {
		Map<String, Object> getAllSongMap = new HashMap<>();
		getAllSongMap.put("total", labelMapper.selectSongTotalByQo(qo));
		getAllSongMap.put("rows", labelMapper.selectSongByCondition(qo));
		return getAllSongMap;
	}

	@Override
	public Map<String, Object> getAllLabel(LableQueryObject qo) {
		Map<String, Object> getAllLabelMap = new HashMap<>();
		getAllLabelMap.put("total", labelMapper.getAllLabelTotal(qo));
		getAllLabelMap.put("rows", labelMapper.getAllLabelList(qo));
		return getAllLabelMap;
	}

	@Override
	@Transactional
	public int saveSongList(Map<String, Object> params) {
		int saveSongList = 0;
		saveSongList = songListMapper.saveSongList(params);

		String songlist_id = (String) params.get("songList_id");
		Map<String, Object> m_ssList = new HashMap<>();
		Date ssList_date = new Date();
		String song_id = (String) params.get("song_id");
		m_ssList.put("songList_id", songlist_id);
		m_ssList.put("song_id", song_id);
		m_ssList.put("ssList_date", ssList_date);

		int saveSsList = SsListMapper.insertAllSong(m_ssList);

		return saveSongList;
	}

	@Override
	public int updateSongList(Map<String, Object> params) {
		int updateSongList = songListMapper.updateSongList(params);
		saveSsListById(params);
		SsListMapper.deleteSong(params);
		return updateSongList;
	}

	@Override
	@Transactional
	public int deleteSongListById(Long songList_id) {
		int deleteSongListById = 0;
		deleteSongListById = songListMapper.deleteSongListById(songList_id);
		int deleteSongById = SsListMapper.deleteAllSong(songList_id);
		int deleteUserSongList = SsListMapper.deleteUserSongList(songList_id);
		return deleteSongListById;
	}

	public int saveSsListById(Map<String, Object> params) {
		String songlist_id = (String) params.get("songList_id");
		Map<String, Object> m_ssList = new HashMap<>();
		Date ssList_date = new Date();
		String song_id = (String) params.get("song_id");
		String[] song_ids = song_id.split(",");

		for (int i = 0; i < song_ids.length; i++) {
			m_ssList.put("songList_id", songlist_id);
			m_ssList.put("song_id", song_ids[i]);
			m_ssList.put("ssList_date", ssList_date);
			int saveSsList = SsListMapper.insertSong(m_ssList);
		}
		return song_ids.length;
	}

	@Override
	public Map<String, Object> getUserBysongList_id(String songList_id) {
		Map<String, Object> getUserBysongList_id = new HashMap<>();
		getUserBysongList_id.put("rows", songListMapper.getUserBysongList_id(songList_id));
		return getUserBysongList_id;
	}

	@Override
	public Map<String, Object> getLabelBysongList_id(String songList_id) {
		List<Map<String, Object>> Labels = songListMapper.selectSongListById(songList_id);
		String label = (String) Labels.get(0).get("songList_label");
		System.out.println(label);
		Map<String, Object> getLabelBysongList_id = new HashMap<>();
		getLabelBysongList_id.put("rows", songListMapper.getLabelBysongList_id(label));

		return getLabelBysongList_id;
	}

	@Override
	public Map<String, Object> getSongBysongList_id(String songList_id) {
		Map<String, Object> getSongBysongList_id = new HashMap<>();
		getSongBysongList_id.put("rows", songListMapper.getSongBysongList_id(songList_id));
		return getSongBysongList_id;
	}

}
